//
//  SDPhotoFilterViewController.m
//  SKinDiary
//
//  Created by pcyang on 16/7/29.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDPhotoFilterViewController.h"
#import "SDPhotoModelCell.h"
#import "SDPhotoPositionCell.h"
#import "SDCameraStatusCell.h"
#import "SDPhotoCustomTagCell.h"
#import "VPCommentInputBar.h"

@interface SDPhotoFilterViewController()<VPCommentInputBarDelegate>
@property(nonatomic,strong)SDPhotoCustomTagCell* customTagView;
@property(nonatomic,strong)SDPhotoModelCell* modeView;
@property(nonatomic,strong)SDPhotoPositionCell* positionView;
@property(nonatomic,strong)SDCameraStatusCell* cameraStatusView;
@end

@implementation SDPhotoFilterViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initViews];
}

- (void)initViews
{
    BOOL naviHidden = [self navigationBarHidden];
    if (!naviHidden && self.navBar != nil)
    {
        [self.navBar setTitle:@"照片筛选"];
        UIImage* addImg = LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"actionbar_btn_back.png");
        [self.navBar customNaviLeftButton:self action:@selector(onBack:) image:addImg];
        [self.navBar customNaviRightButton:self action:@selector(onStore:) title:@"确定"];
    }
    
    _customTagView = [[SDPhotoCustomTagCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"customTagView"];
    _customTagView.frame = CGRectMake(0, kNavBarHeight, kScreenWidth, kPhotoCustomTagCellHeight - 20);
  //  _customTagView.frame = CGRectMake(0, kNavBarHeight, kScreenWidth,  150);
    [_customTagView.addButton addTarget:self action:@selector(onAddTagButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

    _customTagView.addButton.top = (_customTagView.height - _customTagView.addButton.height)/2;
    _customTagView.titleLabel.top = (_customTagView.height - _customTagView.titleLabel.height)/2;
    [_customTagView.addButton setHitTestEdgeInsets:UIEdgeInsetsMake(-44, -44, -44, -44)];
    [_customTagView setCellData:_filterItem.tag];
    _customTagView.bottomLineView.top = _customTagView.height - 1;
    [self.view addSubview:_customTagView];
    
    CGFloat modeViewH = [SDPhotoModelCell getRequiredCellHeight:[SDPhotoModelCell getTagItems]];
    _modeView = [[SDPhotoModelCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"modlView"];
    _modeView.frame = CGRectMake(0, _customTagView.bottom + 5, kScreenWidth, modeViewH);
    [_modeView setCellData:_filterItem.mode];
    [self.view addSubview:_modeView];
    
    CGFloat positionViewH = [SDPhotoPositionCell getRequiredCellHeight:[SDPhotoPositionCell getTagItems]];
    _positionView = [[SDPhotoPositionCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"positionView"];
    _positionView.frame = CGRectMake(0, _modeView.bottom + 5, kScreenWidth, positionViewH);
    [_positionView setCellData:_filterItem.position];
    _positionView.contentView.frame = _positionView.bounds;
    [self.view addSubview:_positionView];
    
    CGFloat cameraStatusViewH = [SDCameraStatusCell getRequiredCellHeight:[SDCameraStatusCell getTagItems]];
    _cameraStatusView = [[SDCameraStatusCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cameraStatusView"];
    _cameraStatusView.frame = CGRectMake(0, _positionView.bottom + 5, kScreenWidth, cameraStatusViewH);
    [_cameraStatusView setCellData:_filterItem.status];
    _cameraStatusView.contentView.frame = _cameraStatusView.bounds;
    [self.view addSubview:_cameraStatusView];
    
}

- (BOOL)navigationBarHidden
{
    return NO;
}

#pragma mark - ui event
-(void)onAddTagButtonClicked:(id)sender
{
    if ([_customTagView.tagItemsArray count] >= 1) {
        [SDAlertManagement showNormalErrorAlert:@"最多可以添加一个标签"];
        return;
    }
    
    [[VPCommentInputBar sharedInstance] setMaxLength:8];
    [VPCommentInputBar showInputBarAddedTo:self.view delegate:self placeHolderText:@"添加新的标签..."];
    
}

- (void)onBack:(id)sender
{
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)onStore:(id)sender
{
    if (!_filterItem) {
        _filterItem = [[SDPhotoFilterItem alloc] init];
    }
    NSArray* tags = [_customTagView getSelectedTagItems];
    if (CHECK_VALID_ARRAY(tags)) {
        _filterItem.tag =  [tags componentsJoinedByString:@","];
    } else {
        _filterItem.tag = nil;
    }
    
    tags = [_modeView getSelectedTagItems];
    if (CHECK_VALID_ARRAY(tags)) {
        _filterItem.mode =  [tags componentsJoinedByString:@","];
    } else {
        _filterItem.mode = nil;
    }
    
    tags = [_positionView getSelectedTagItems];
    if (CHECK_VALID_ARRAY(tags)) {
        _filterItem.position =  [tags componentsJoinedByString:@","];
    } else {
        _filterItem.position = nil;
    }
    
    tags = [_cameraStatusView getSelectedTagItems];
    if (CHECK_VALID_ARRAY(tags)) {
        _filterItem.status =  [tags componentsJoinedByString:@","];
    } else {
        _filterItem.status = nil;
    }
    
    if (self.block != nil) {
        self.block(_filterItem);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - block
- (void)didFilterItem:(SelectedItem)block {
    self.block = block;
}

#pragma mark- VPCommentInputBarDelegate
- (BOOL)commentInputBarShouldBeginEditing:(VPCommentInputBar *)inputBar
{
    return YES;
}

- (void)commentInputBarDidPressSendButton:(VPCommentInputBar *)inputBar
{
    NSString* content = [inputBar getTextContent];
    
    if (0 < [content length])
    {
        PhotoTagItem* tagItem = [[PhotoTagItem alloc] init];
        tagItem.title = content;
        [_customTagView addTagItem:tagItem];
    }
}

- (void)commentInputBar:(VPCommentInputBar *)inputBar textViewDidChange:(UITextView *)textView
{
    
}
@end
